package com.eshop.modules.quartz.service.impl;

import java.io.IOException;
import java.util.Iterator;
import com.eshop.utils.FileUtil;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import com.eshop.modules.quartz.service.dto.QuartzLogDto;
import java.util.LinkedHashMap;
import java.util.List;
import com.github.pagehelper.PageInfo;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.modules.quartz.service.dto.QuartzLogQueryCriteria;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.modules.quartz.service.QuartzLogService;
import com.eshop.modules.quartz.domain.QuartzLog;
import com.eshop.modules.quartz.service.mapper.QuartzLogMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class QuartzLogServiceImpl extends BaseServiceImpl<QuartzLogMapper, QuartzLog> implements QuartzLogService
{
    private final IGenerator generator;
    
    public Map<String, Object> queryAll(final QuartzLogQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<QuartzLog> page = (PageInfo<QuartzLog>)new PageInfo((List)this.queryAll(criteria));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)QuartzLogDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<QuartzLog> queryAll(final QuartzLogQueryCriteria criteria) {
        return (List<QuartzLog>)((QuartzLogMapper)this.baseMapper).selectList(QueryHelpPlus.getPredicate(QuartzLog.class, criteria));
    }
    
    public void download(final List<QuartzLogDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final QuartzLogDto quartzLog : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put(" baenName", quartzLog.getBaenName());
            map.put(" createTime", quartzLog.getCreateTime());
            map.put(" cronExpression", quartzLog.getCronExpression());
            map.put(" exceptionDetail", quartzLog.getExceptionDetail());
            map.put(" isSuccess", quartzLog.getIsSuccess());
            map.put(" jobName", quartzLog.getJobName());
            map.put(" methodName", quartzLog.getMethodName());
            map.put(" params", quartzLog.getParams());
            map.put(" time", quartzLog.getTime());
            map.put("任务名称", quartzLog.getBaenName());
            map.put("Bean名称 ", quartzLog.getCreateTime());
            map.put("cron表达式", quartzLog.getCronExpression());
            map.put("异常详细 ", quartzLog.getExceptionDetail());
            map.put("状态", quartzLog.getIsSuccess());
            map.put("任务名称", quartzLog.getJobName());
            map.put("方法名称", quartzLog.getMethodName());
            map.put("参数", quartzLog.getParams());
            map.put("耗时（毫秒）", quartzLog.getTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public QuartzLogServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
}
